package com.myMinistry.gui;

import java.text.SimpleDateFormat;
import java.util.Calendar;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.myMinistry.R;

public class DialogFragTimeSelector extends DialogFragment {
	public static final int TIME_START = 1;
	public static final int TIME_END = 2;
	
	public static DialogFragTimeSelector newInstance(String title, int which, int hour, int minutes) {
    	DialogFragTimeSelector frag = new DialogFragTimeSelector();
        Bundle args = new Bundle();
        args.putString("title", title);
        args.putInt("which", which);
        args.putInt("hour", hour);
        args.putInt("minutes", minutes);
        frag.setArguments(args);
        return frag;
    }
	
	@Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
		final SimpleDateFormat timeFormat = new SimpleDateFormat("h:mm");
		final Calendar time = Calendar.getInstance();
		time.set(Calendar.HOUR_OF_DAY, getArguments().getInt("hour"));
		time.set(Calendar.MINUTE, getArguments().getInt("minutes"));
		final View view = LayoutInflater.from(getActivity()).inflate(R.layout.time_picker, null);
		final TextView timeDisplay = (TextView) view.findViewById(R.id.timeDisplay);
		timeDisplay.setText(timeFormat.format(time.getTime()).toString());
    	final Button amPM = (Button) view.findViewById(R.id.amPM);
    	
    	amPM.setText((time.get(Calendar.AM_PM) == Calendar.PM) ? R.string.pm : R.string.am);
    	amPM.setOnClickListener(new View.OnClickListener() {
    		@Override
			public void onClick(View v) {
    			if(time.get(Calendar.AM_PM) == Calendar.PM) {
    				time.add(Calendar.HOUR_OF_DAY, -12);
    				amPM.setText(R.string.am);
    			}
    			else {
    				time.add(Calendar.HOUR_OF_DAY, 12);
    				amPM.setText(R.string.pm);
    			}
			}
		});
		
    	view.findViewById(R.id.twelve).setOnClickListener(new View.OnClickListener() {
    		@Override
			public void onClick(View v) {
    			time.set(Calendar.HOUR_OF_DAY, (time.get(Calendar.AM_PM) == Calendar.AM) ? 0 : 12);
    			timeDisplay.setText(timeFormat.format(time.getTime()).toString());
			}
		});
    	
    	view.findViewById(R.id.one).setOnClickListener(new View.OnClickListener() {
    		@Override
			public void onClick(View v) {
    			time.set(Calendar.HOUR_OF_DAY, (time.get(Calendar.AM_PM) == Calendar.AM) ? 1 : 13);
    			timeDisplay.setText(timeFormat.format(time.getTime()).toString());
			}
		});
    	
    	view.findViewById(R.id.two).setOnClickListener(new View.OnClickListener() {
    		@Override
			public void onClick(View v) {
    			time.set(Calendar.HOUR_OF_DAY, (time.get(Calendar.AM_PM) == Calendar.AM) ? 2 : 14);
    			timeDisplay.setText(timeFormat.format(time.getTime()).toString());
			}
		});
    	
    	view.findViewById(R.id.three).setOnClickListener(new View.OnClickListener() {
    		@Override
			public void onClick(View v) {
    			time.set(Calendar.HOUR_OF_DAY, (time.get(Calendar.AM_PM) == Calendar.AM) ? 3 : 15);
    			timeDisplay.setText(timeFormat.format(time.getTime()).toString());
			}
		});
    	
    	view.findViewById(R.id.four).setOnClickListener(new View.OnClickListener() {
    		@Override
			public void onClick(View v) {
    			time.set(Calendar.HOUR_OF_DAY, (time.get(Calendar.AM_PM) == Calendar.AM) ? 4 : 16);
    			timeDisplay.setText(timeFormat.format(time.getTime()).toString());
			}
		});
    	
    	view.findViewById(R.id.five).setOnClickListener(new View.OnClickListener() {
    		@Override
			public void onClick(View v) {
    			time.set(Calendar.HOUR_OF_DAY, (time.get(Calendar.AM_PM) == Calendar.AM) ? 5 : 17);
    			timeDisplay.setText(timeFormat.format(time.getTime()).toString());
			}
		});
    	
    	view.findViewById(R.id.six).setOnClickListener(new View.OnClickListener() {
    		@Override
			public void onClick(View v) {
    			time.set(Calendar.HOUR_OF_DAY, (time.get(Calendar.AM_PM) == Calendar.AM) ? 6 : 18);
    			timeDisplay.setText(timeFormat.format(time.getTime()).toString());
			}
		});
    	
    	view.findViewById(R.id.seven).setOnClickListener(new View.OnClickListener() {
    		@Override
			public void onClick(View v) {
    			time.set(Calendar.HOUR_OF_DAY, (time.get(Calendar.AM_PM) == Calendar.AM) ? 7 : 19);
    			timeDisplay.setText(timeFormat.format(time.getTime()).toString());
			}
		});
    	
    	view.findViewById(R.id.eight).setOnClickListener(new View.OnClickListener() {
    		@Override
			public void onClick(View v) {
    			time.set(Calendar.HOUR_OF_DAY, (time.get(Calendar.AM_PM) == Calendar.AM) ? 8 : 20);
    			timeDisplay.setText(timeFormat.format(time.getTime()).toString());
			}
		});
    	
    	view.findViewById(R.id.nine).setOnClickListener(new View.OnClickListener() {
    		@Override
			public void onClick(View v) {
    			time.set(Calendar.HOUR_OF_DAY, (time.get(Calendar.AM_PM) == Calendar.AM) ? 9 : 21);
    			timeDisplay.setText(timeFormat.format(time.getTime()).toString());
			}
		});
    	
    	view.findViewById(R.id.ten).setOnClickListener(new View.OnClickListener() {
    		@Override
			public void onClick(View v) {
    			time.set(Calendar.HOUR_OF_DAY, (time.get(Calendar.AM_PM) == Calendar.AM) ? 10 : 22);
    			timeDisplay.setText(timeFormat.format(time.getTime()).toString());
			}
		});
    	
    	view.findViewById(R.id.eleven).setOnClickListener(new View.OnClickListener() {
    		@Override
			public void onClick(View v) {
    			time.set(Calendar.HOUR_OF_DAY, (time.get(Calendar.AM_PM) == Calendar.AM) ? 11 : 23);
    			timeDisplay.setText(timeFormat.format(time.getTime()).toString());
			}
		});
    	
    	view.findViewById(R.id.zero).setOnClickListener(new View.OnClickListener() {
    		@Override
			public void onClick(View v) {
    			time.set(Calendar.MINUTE,0);
    			timeDisplay.setText(timeFormat.format(time.getTime()).toString());
			}
		});
    	
    	view.findViewById(R.id.fifteen).setOnClickListener(new View.OnClickListener() {
    		@Override
			public void onClick(View v) {
    			time.set(Calendar.MINUTE,15);
    			timeDisplay.setText(timeFormat.format(time.getTime()).toString());
			}
		});
    	
    	view.findViewById(R.id.thirty).setOnClickListener(new View.OnClickListener() {
    		@Override
			public void onClick(View v) {
    			time.set(Calendar.MINUTE,30);
    			timeDisplay.setText(timeFormat.format(time.getTime()).toString());
			}
		});
    	
    	view.findViewById(R.id.fourtyfive).setOnClickListener(new View.OnClickListener() {
    		@Override
			public void onClick(View v) {
    			time.set(Calendar.MINUTE,45);
    			timeDisplay.setText(timeFormat.format(time.getTime()).toString());
			}
		});

    	AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    	builder.setView(view)
    	.setTitle(getArguments().getString("title"))
    	.setIcon(R.drawable.clock)
    	.setPositiveButton(R.string.setTime, new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				if(getActivity() instanceof TimeEditor) {
					if(getArguments().getInt("which") == TIME_START)
						((TimeEditor) getActivity()).updateTimeStart(time.get(Calendar.HOUR_OF_DAY), time.get(Calendar.MINUTE));
					else if(getArguments().getInt("which") == TIME_END)
						((TimeEditor) getActivity()).updateTimeEnd(time.get(Calendar.HOUR_OF_DAY), time.get(Calendar.MINUTE));
				}
			}
		})
		.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				/** Do nothing */
			}
		});
    	return builder.create();
    }
}